package codename.carrot.bomberman.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;
import codename.carrot.bomberman.AppStorage;
import codename.carrot.bomberman.LevelDefinition;
import codename.carrot.bomberman.LevelReader;
import codename.carrot.bomberman.R;
import codename.carrot.bomberman.adapters.LevelAdapter;

/**
 *  Activity that displays all levels in a {@link Gallery} , and lets the host select a level by clicking on 
 *  one of the levels.
 *  @author Group A4
 *
 */

public class LevelViewerActivity extends Activity implements OnItemClickListener {
	/**
	 * The name of the extra data to be send with the intent on activity finish.
	 */
	public final static String RESULT_LEVEL_NAME = "level_name";
	/**
	 * The request code to start this activity for a result.
	 */
	public final static int REQUEST_CODE = 3258;
	/**
	 * The adapter containing and used for displaying levels.
	 */
    private LevelAdapter levelAdapter;
	/**
	 * The gallery for displaying level images.
	 */
    private Gallery levelGallery;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level_viewer);

        levelGallery = (Gallery) findViewById(R.id.levelGallery);
        levelAdapter = new LevelAdapter(this, R.layout.level_item, 0);
                
        LevelReader levelReader = new LevelReader(this);
        String[] levels = levelReader.getLevelNames();
        for(String s : levels) {
            try {
                levelReader.readLevel(s);
                levelAdapter.add(levelReader.getLevelDefinition());
            } catch(Exception e) {}
        }
        levelGallery.setAdapter(levelAdapter);
        levelGallery.setOnItemClickListener(this);
    }
    
	/**
	 * Set the result of the activity to cancelled and finish.
	 */
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        finish();
    }
    
	/**
	 * Setting up the screen width and height on resume of the activity.
	 */
    public void onResume() {
        super.onResume();
        AppStorage.SCREEN_WIDTH= getWindowManager().getDefaultDisplay().getWidth();
        AppStorage.SCREEN_HEIGHT = getWindowManager().getDefaultDisplay().getHeight();
    }
    
    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
        LevelDefinition definition = levelAdapter.getItem(position);
        Toast.makeText(this, "You selected: " + definition.getName(), Toast.LENGTH_LONG).show();
        Intent intent = new Intent();
        intent.putExtra(RESULT_LEVEL_NAME, definition.getName());
        setResult(RESULT_OK, intent);
        finish();
    }
}
